package com.megvii.koala.tv.presenter;

import com.android.volley.VolleyError;
import com.megvii.corelib.net.Callback;
import com.megvii.koala.base.KoalaPresenter;
import com.megvii.koala.constance.ErrorCode;
import com.megvii.koala.tv.data.CameraBean;
import com.megvii.koala.tv.data.SurveillanceCameraManager;
import com.megvii.koala.tv.data.TVBoxManager;
import com.megvii.koala.tv.data.TVGlobalData;
import com.megvii.koala.tv.fragment.DisplayCameraSettingFragment;
import com.megvii.koala.tv.fragment.RecognizeCamerasSettingFragment;
import com.megvii.koala.tv.net.TVNetAPI;
import com.megvii.koala.tv.net.tv.GetScreenListResponse;

import java.util.Collection;
import java.util.List;

/**
 * Created by linyue on 16/3/16.
 */
public class RecognizeCamerasSettingPresenter
        extends KoalaPresenter<RecognizeCamerasSettingFragment>
        implements RecognizeCamerasSettingFragment.RecognizeCamerasSettingListener
{
    private SurveillanceCameraManager cameraManager;

    private TVBoxManager boxManager;

    @Override
    protected void onCreated()
    {
        super.onCreated();
        this.cameraManager = TVGlobalData.instance().cameraManager;
        this.boxManager = TVGlobalData.instance().boxManager;
        viewHolder.setListener(this);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        requestGetScreenList();
    }

    private void requestGetScreenList()
    {
        String boxToken = boxManager.getSelectedToken();
        viewHolder.showProgressBar();
        TVNetAPI.requestGetScreenList(boxToken, this, this);
    }

    @Callback(clazz = GetScreenListResponse.class)
    public void onGetScreenListResponse(GetScreenListResponse response) {
        viewHolder.hideProgressBar();
        if (response.code == ErrorCode.NO_ERROR) {
            Collection<String> mapIds = cameraManager.getRecognizeCameraIds();
            cameraManager.clear();
            cameraManager.update(response.data.screens);
            List<CameraBean> cameras = cameraManager.getAll();
            viewHolder.updateView(cameras);
            //判断服务器取到的摄像头列表中是否含有之前选定的摄像头
            if (mapIds != null) {
                boolean has_cameras = false;
                for (CameraBean camNet :
                        cameras) {
                    if (mapIds.contains(camNet.id)) {
                        //网络列表中有本地配置的摄像头
                        viewHolder.selectExistsCam(camNet.id);
                        has_cameras = true;
                    }
                }
                //在这里还需要进行判断，若网络回传数据中没有本地已经设置的摄像头，则将所有的配置文件保存取消.
                if(!has_cameras){
                    cameraManager.clear();
                    cameraManager.persistentClear();
                }
            } else {
                //网络中没有本地摄像头
                cameraManager.clear();
                cameraManager.persistentClear();
                //TODO 这里应该进行跳转到选择主机界面，给一个定时器
            }
        }
        else
        {
            showToast(ErrorCode.getDescription(response.code));
        }
    }

    @Override
    public void onClickSave(List<CameraBean> selectedCameras)
    {
        if (selectedCameras != null && !selectedCameras.isEmpty())
        {
            //首先清空之前选择的
            cameraManager.clearRecognizeCameras();
            cameraManager.addRecognizeCameras(selectedCameras);
            viewHolder.startFragment(DisplayCameraSettingFragment.class, null);
        }
        else
        {
            showToast("请选择至少一个视频流");
        }
    }

    @Override
    public void onBackPressed()
    {
        viewHolder.finish();
    }

    @Override
    public void onClickRefreshNetwork()
    {
        super.onClickRefreshNetwork();
        viewHolder.setMainContainerVisible(true);
        requestGetScreenList();
    }

    @Override
    public void onErrorResponse(VolleyError error)
    {
        super.onErrorResponse(error);
        viewHolder.setMainContainerVisible(false);
    }
}
